<template>
  <div class='list' ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrap">
            <div class="button">{{this.$store.state.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrap"
            v-for='item of hotCities'
            :key='item.id'
            @click = 'handleCityClick(item.name)'
          >
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item,index) of cities" :key='item.id' :ref='index'>
        <div class="title border-topbottom">{{index}}</div>
        <div class="itemList">
          <div
            class="item border-bottom"
            v-for='innerItem of item'
            :key='innerItem.id'
            @click = 'handleCityClick(innerItem.name)'
          >
            {{innerItem.name}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
export default{
  name: 'cityList',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  methods: {
    handleCityClick (city) {
      this.$store.dispatch('changeCity', city)
      this.$router.push('/')
    }
  },
  watch: {
    letter () {
      console.log(this.$refs[this.letter][0])
      const ele = this.$refs[this.letter][0]
      this.scroll.scrollToElement(ele)
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true
    })
  }
}
</script>
<style lang='stylus' scoped>
.list
    overflow: hidden
    position: absolute
    top: 1.48rem
    right: 0
    bottom: 0
    left: 0
  .title
    height: .48rem
    line-height: .48rem
    background: #eee
    padding-left: .1rem
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .button-list
    padding: .1rem .3rem .1rem .1rem
    overflow: hidden
    .button-wrap
      float: left
      width: 33.3%
      .button
        border: .02rem solid #eee
        margin: .1rem
        text-align: center
        padding: .1rem
        border-radius: .08rem
  .border-bottom
    &:before
      border-color: #ccc
  .item
    line-height: .44rem
    padding: .05rem 0 .05rem .1rem
</style>
